package nautilus.lab;

import nautilus.lab.activity.GLES20VBOTest;
import nautilus.lab.activity.MyGalleryActivity;
import nautilus.lab.activity.MyTestActivity1;
import nautilus.lab.activity.ScreenSlidePageActivity;
import nautilus.lab.activity.SnappingHScrollActivity;
import nautilus.lab.activity.WaveViewerActivity;
import nautilus.service.DataService;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity 
{
	private Button btnTest1, btnTest2, btnTest3, btnStartBluetooth;
	private EditText txtServerHost, txtPort;
	private TextView lbDeviceName;
	
	public static final int TCPCONNECT = 1;
	public static final int BLUECONNECT = 2;
	private static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnTest1 = (Button)findViewById(R.id.button1);
		btnTest1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startAnotherActivity();
			}
		});
		
		btnTest2 = (Button)findViewById(R.id.button2);
		btnTest2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startTest2Activity();
			}
		});
		
		btnTest3 = (Button)findViewById(R.id.button3);
		btnTest3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startWaveActivity();
			}
		});
		
		txtServerHost = (EditText)findViewById(R.id.txtServerHost);
		txtPort = (EditText)findViewById(R.id.txtPort);
		txtServerHost.setText("192.168.0.100");
		txtPort.setText("9909");
		
		btnStartBluetooth = (Button)findViewById(R.id.btnStartBluetooth);
		btnStartBluetooth.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		lbDeviceName = (TextView)findViewById(R.id.lbRemoteDevice);
		
	}
	
	private void startAnotherActivity()
	{
		//Intent intent = new Intent(this, GLES20VBOTest.class);
		//startActivity(intent);
		
		//startActivity(new Intent(this, ScreenSlidePageActivity.class));
		//startActivity(new Intent(this, MyGalleryActivity.class));
		startActivity(new Intent(this, SnappingHScrollActivity.class));
	}
	
	private void startWaveActivity()
	{
		String serverHost = txtServerHost.getText().toString();
		String strPort = txtPort.getText().toString();
		int port = Integer.parseInt(strPort);
		Intent intentService = new Intent(this, DataService.class);
		intentService.putExtra("requested_service", TCPCONNECT);
		intentService.putExtra("server_host", serverHost);
		intentService.putExtra("port", port);
		startService(intentService);
		
		Intent intent = new Intent(this, WaveViewerActivity.class);
		startActivity(intent);
	}
	
	private void startTest2Activity()
	{
		Intent intent = new Intent(this, MyTestActivity1.class);
		startActivity(intent);
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
	        case REQUEST_CONNECT_DEVICE:
	            // When DeviceListActivity returns with a device to connect
	            if (resultCode == Activity.RESULT_OK) {
	                // Get the device MAC address
	            	lbDeviceName.setText(data.getExtras().getString("selected_device"));
	                // Get the BLuetoothDevice object
	                //BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
	                // Attempt to connect to the device
	                //mCommandService.connect(device);
	            }
	            break;
	        case REQUEST_ENABLE_BT:
	            // When the request to enable Bluetooth returns
	            if (resultCode == Activity.RESULT_OK) {
	                // Bluetooth is now enabled, so set up a chat session
	                //setupCommand();
	            } else {
	                // User did not enable Bluetooth or an error occured
	                //Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
	                //finish();
	            }
        }
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.scan:
            // Launch the DeviceListActivity to see devices and do scan
        	
            return true;
        case R.id.discoverable:
            // Ensure this device is discoverable by others
            return true;
        }
        return false;
    }

}
